package io.gitee.opabinia.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.query.DefaultQuery;
import java.util.Collections;
import org.apache.ibatis.type.JdbcType;

/**
 * Created by IntelliJ IDEA.
 *
 * @author eric 2024/9/24 11:55
 */
public class MysqlGenerator {

  public static void main(String[] args) {
    final String url = "jdbc:mysql://127.0.0.1:3306/lann?remarks=true&useInformationSchema=true";
    final String outputDir = "C:\\d\\CodeSrc\\";

    FastAutoGenerator.create(url, "root", "root")
        .dataSourceConfig(builder ->
            builder.databaseQueryClass(DefaultQuery.class)
                .typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                      // 兼容旧版本转换成Integer
                      if (JdbcType.TINYINT == metaInfo.getJdbcType()) {
                        return DbColumnType.INTEGER;
                      }
                      return typeRegistry.getColumnType(metaInfo);
                    }
                ))
        .globalConfig(builder -> builder
            .author("caesar")
            .outputDir(outputDir)
            .commentDate("yyyy-MM-dd")
        )
        .packageConfig(builder -> builder
            .parent("com.lannlife.platform.common.master")
            .entity("entity")
            .mapper("mapper")
            .service("dao")
            .serviceImpl("dao.impl")
            .xml("mapper.xml")
            .pathInfo(Collections.singletonMap(OutputFile.xml, outputDir))
        )
        .strategyConfig(builder -> builder
            .addInclude(
                "mt_refresh_token_log"
            )
            .entityBuilder()
            .enableTableFieldAnnotation()
            .idType(IdType.AUTO)
            .enableLombok()
            .serviceBuilder()
            .convertServiceFileName(entityName -> entityName + "Dao")
            .convertServiceImplFileName(entityName -> entityName + "DaoImpl")
            .mapperBuilder()
        )
        .templateEngine(new VelocityTemplateEngine())
        .execute();
  }

}
